# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=bacula
pkgver=13.0.3
pkgrel=0
pkgdesc="Enterprise ready, network based backup program"
url="https://www.bacula.org/"
arch="all"
license="AGPL-3.0-or-later WITH OpenSSL-Exception"
makedepends="autoconf automake libtool mariadb-connector-c-dev ncurses-dev openssl-dev>3 libpq-dev
	sqlite-dev zlib-dev lzo-dev acl-dev"
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="$pkgname-doc $pkgname-mysql $pkgname-pgsql $pkgname-sqlite
	$pkgname-client-openrc:client_openrc $pkgname-client $pkgname-libs
	$pkgname-openrc"
pkgusers="bacula"
pkggroups="bacula"
somask="libbaccats-$pkgver.so"
source="https://downloads.sourceforge.net/project/bacula/bacula/$pkgver/bacula-$pkgver.tar.gz
	bacula-dir.initd
	bacula-sd.initd
	bacula-fd.initd
	bacula-7.4.0-path-mounted.patch
	bacula-11.0.6-pthread-double-detach-fix.patch
	"
options="!check" #no test suite provided

_bworkdir="/var/lib/$pkgname"

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc/$pkgname \
		--localstatedir=/var \
		--mandir=/usr/share/man \
		--docdir=/usr/share/doc/bacula \
		--infodir=/usr/share/info \
		--with-pid-dir=/run/$pkgname \
		--with-subsys-dir=/run/lock/subsys \
		--with-logdir=/var/log/$pkgname \
		--enable-largefile \
		--enable-smartalloc \
		--disable-nls \
		--with-openssl=/usr/include/openssl \
		--with-postgresql \
		--with-mysql \
		--with-sqlite3 \
		--with-scriptdir=/etc/$pkgname/scripts \
		--with-working-dir=$_bworkdir \
		--with-dir-user=$pkgname \
		--with-dir-group=$pkgname \
		--with-archivedir=$_bworkdir/archive \
		--with-sd-user=$pkgusers \
		--with-sd-group=$pkggroups \
		--with-fd-user=root \
		--with-fd-group=root \
		--with-sbin-perm=0755

	make NO_ECHO=
}

package() {
	make DESTDIR="$pkgdir" install

	local daemon; for daemon in dir sd fd; do
		install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
			"$pkgdir"/etc/init.d/$pkgname-$daemon
	done

	install -Dm644 examples/sample-query.sql \
		"$pkgdir"/etc/$pkgname/scripts/query.sql

	# Fix correct log dir
	sed -i 's%/var/bacula/log%/var/log/bacula%' \
		"$pkgdir"/etc/$pkgname/$pkgname-dir.conf

	# Install logrotate script
	install -Dm644 scripts/logrotate $pkgdir/etc/logrotate.d/$pkgname
	sed -i 's%/var/bacula/log%/var/log/bacula%' \
		"$pkgdir"/etc/logrotate.d/$pkgname

	# Use shell script for catalog backup instead of perl
	sed -i 's/make_catalog_backup.pl/make_catalog_backup/' \
		"$pkgdir"/etc/$pkgname/$pkgname-dir.conf

	local dir; for dir in $_bworkdir \
		/etc/$pkgname/scripts/make_catalog_backup* \
		/etc/$pkgname/scripts/mtx-changer* \
		/etc/$pkgname/*.conf
	do
		chown $pkgusers.$pkggroups "$pkgdir"$dir
	done

	find "$pkgdir"/usr/lib -iname libbaccats* -type l -delete

	# resolves conflict with community/bat
	# this manpage is for a program that isn't built here
	rm "$pkgdir"/usr/share/man/man1/bat.1*
}

_mv_backend() {
	mkdir -p "$subpkgdir"/usr/lib
	mkdir -p "$subpkgdir"/etc/$pkgname/scripts

	mv "$pkgdir"/usr/lib/libbaccats-$1-$pkgver.so \
		"$subpkgdir"/usr/lib

	ln -s libbaccats-$pkgver.so "$subpkgdir"/usr/lib/libbaccats.so
	ln -s libbaccats-$1-$pkgver.so \
		"$subpkgdir"/usr/lib/libbaccats-$pkgver.so

	mv "$pkgdir"/etc/$pkgname/scripts/*_$1_* \
		"$subpkgdir"/etc/$pkgname/scripts
}

mysql() {
	pkgdesc="Bacula MySQL backend"
	depends="$pkgname"

	_mv_backend mysql
}

pgsql() {
	pkgdesc="Bacula PGSQL backend"
	depends="$pkgname"

	_mv_backend postgresql
}

sqlite() {
	pkgdesc="Bacula SQLite backend"
	depends="$pkgname"

	_mv_backend sqlite3
}

libs() {
	pkgdesc="Bacula libraries"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/* "$subpkgdir"/usr/lib
}

client_openrc() {
	pkgdesc="Bacula client init script"
	install_if="openrc $pkgname-client=$pkgver-r$pkgrel"
	install -D -m755 "$pkgdir"/etc/init.d/$pkgname-fd \
		"$subpkgdir"/etc/init.d/$pkgname-fd
}

client() {
	pkgdesc="Bacula client"
	mkdir -p "$subpkgdir"/usr/sbin \
		"$subpkgdir"/etc/$pkgname
	for bin in $pkgname $pkgname-fd bconsole btraceback; do
		mv "$pkgdir"/usr/sbin/$bin "$subpkgdir"/usr/sbin
	done
	mv "$pkgdir"/etc/$pkgname/$pkgname-fd.conf "$subpkgdir"/etc/$pkgname/
	install -d -o $pkgname -g $pkgname "$subpkgdir"/$_bworkdir
}

sha512sums="
59ec616535959bde26b5b03a60355f3f8d80ae7a719a079d354f41c0f103b0e467177b6f813ef3eb4d30866f73c393d766e8337e20bb97f19ffd7eba5e5c59c5  bacula-13.0.3.tar.gz
bb954bb94bffa68ba80872046782a73012487291f019fb8d7ff77fc7f4325e25bee88612b3279b4db7d3c2002ac7448ffabcda62da7bab54642a7413904abc91  bacula-dir.initd
0505b9a74520af6982d5df4390525976f8e009e641d0acfe8f24b2ec9f155166a65eecbabee9ff70ffe188d523effa64f958acd8f657410b1c002a6d736e8aee  bacula-sd.initd
91a53cb566cfd91b42a319bd6ff9947047cae187c6ec198767853d1a0253667d6c2c58f0e72ed43a7b9f40b76e705cafde7a1d6ac86577fa0f64f44b031dec11  bacula-fd.initd
2d3757236aacca421261a8866ff04b5b0151538e1462559bd1240119b1bece1d456acbba9fee86dbc6aaec7af2a52eb2c0b7490c5f371b7deb478731c74342ff  bacula-7.4.0-path-mounted.patch
775ff4449ac7164cfc0f303c6aeb2be1b2d08f4197ac89db453795185c3251e94ccb6ef27068b7802453845d6bcf8474785a1ac7254227f027743baa2ab754bc  bacula-11.0.6-pthread-double-detach-fix.patch
"
